<!DOCTYPE HTML>
<html>
<head>
    <title>商品分类管理</title>
    <link rel="stylesheet" href="/stylesheets/style.css">
    <script src="/scripts/jquery.min.js"></script>
    <link rel="stylesheet" href="/scripts/miniui/themes/default/miniui.css">
    <link rel="stylesheet" href="/scripts/miniui/themes/icons.css">
    <link rel="stylesheet" href="/editor/common.css">
    <link rel="stylesheet" href="/scripts/miniui/themes/bootstrap/skin.css">
    <script src="/scripts/miniui/miniui.js"></script>
</head>
<body  style="padding: 0;width: 100%;height: 100%">
<div class="mini-splitter" style="width:100%;height:97%;">
    <div size="240" showCollapseButton="true">
        <div class="mini-fit">
            <ul id="tree1" class="mini-tree"  url="/getdata/parentGoodsTypeTree" style="width:100%;"
                showTreeIcon="true" textField="name" idField="id" parentField="pid" resultAsTree="false"
            >
            </ul>
        </div>
    </div>
    <div showCollapseButton="true">
        <div style="width:99%;">
            <div class="mini-toolbar" style="border-bottom:0;padding:0px;">
                <table style="width:100%;">
                    <tr>
                        <td style="width:100%;">
                            <a class="mini-button" iconCls="icon-add" onclick="add()">增加</a>
                            <a class="mini-button" iconCls="icon-edit" onclick="edit()">编辑</a>
                            <a class="mini-button" iconCls="icon-remove" onclick="remove()">删除</a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div id="datagrid1" class="mini-datagrid" style="width:99%;height: 82%;"
             url="/getdata/getGoodsType" idField="id" allowResize="true"
             sizeList="[20,30,50,100]" pageSize="30"
        >
            <div property="columns">
                <div type="checkcolumn" width="10"></div>
                <div field="objectId" width="100" align="center" headerAlign="center" allowSort="true">ID</div>
                <div field="parentId" width="100" align="center" headerAlign="center" allowSort="true" >父分类ID</div>
                <div field="name"  width="50" align="center" headerAlign="center" allowSort="true">分类名称</div>
                <div field="sortNum"  width="50" align="center" headerAlign="center" allowSort="true">排序</div>
                <div field="createdAt" width="100" align="center" headerAlign="center"  allowSort="true">创建日期</div>
                <div field="updatedAt" width="100" align="center" headerAlign="center"  allowSort="true">更新日期</div>
            </div>
        </div>
    </div>
</div>
</body>
<script type="text/javascript">
  var flag = 1;
  mini.parse();
  var grid = mini.get("datagrid1");
  var tree = mini.get("tree1");
  var goodType = '商品分类'
  var parentId = ''
  var parentName = ''
  tree.on("nodeselect", function (e) {
    parentId = ''
    parentName = ''
    if (e.isLeaf) {
      if (e.node.objectId){
        parentId = e.node.objectId
      } else {
        parentId = ''
      }
      if (e.node.name){
        parentName = e.node.name
      } else {
        parentName = ''
      }
      if(e.node.objectId){
        grid.load({
          parentId:e.node.objectId,
        });
      }else{
        grid.load();
      }
    } else {

      grid.load();
      grid.setData([]);
      grid.setTotalCount(0);
    }
  });
  grid.load();
  grid.on("drawcell", function (e) {
    var  value = e.value;
  });
  function add() {
    if (parentId && parentId != ''){
      mini.open({
        url: "/getdata/goodsTypeMangerAddForChild?parentId="+parentId+"&parentName="+parentName,
        title: "添加子分类", width: 500, height: 200,
        ondestroy: function (action) {
          grid.reload();
          // parentId = ''
          // parentName = ''
        }
      });
    } else {
      mini.open({
        url: "/getdata/goodsTypeMangerAddForParent",
        title: "添加父分类", width: 500, height: 200,
        ondestroy: function (action) {
          tree.reload();
          parentId = ''
          parentName = ''
        }
      });
    }

  }
  function edit() {
    var row = grid.getSelected();
    if (row) {
      mini.open({
        url: "/getdata/goodsTypeMangerEdit?objectId="+row.objectId+"&name="+row.name,
        title: "编辑商品信息", width: 400, height: 200,
        ondestroy: function (action) {
          grid.reload();
          if (parentId == ''){
            tree.reload();
          }
        }
      });
    } else {
      alert("请选中一条记录");
    }
  }
  function remove() {
    var row = grid.getSelected();
    if (row) {
      $.ajax({
        url:'/getdata/delGoodsType',
        type:'POST',
        data:{ objectId:row.objectId },
        success:function (result) {
          if(result){
            grid.reload();
            if (parentId == ''){
              tree.reload();
            }
          }else{
            alert("删除失败");
          }
        }
      })
    }else {
      alert("请选中一条记录");
    }
  }
</script>
</html>